<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="所属牧场:" name="ranchId">
              <a-select
                show-search
                v-model:value="where.ranchId"
                placeholder="请选择所属牧场"
                style="width: 205px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="灭杀场所:">
              <a-select
                show-search
                v-model:value="where.regionTarget"
                style="width: 120px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option value='1'>牛场</a-select-option>
                <a-select-option value='2'>鸡鸭厂</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="灭杀目标:">
              <a-select
                show-search
                v-model:value="where.killTarget"
                style="width: 120px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option value='1'>灭鼠</a-select-option>
                <a-select-option value='2'>灭蝇</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="服务类型:">
              <a-select
                show-search
                v-model:value="where.serviceType"
                style="width: 120px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option value='1'>首次服务</a-select-option>
                <a-select-option value='2'>紧急服务</a-select-option>
                <a-select-option value='3'>常规服务</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="bsrId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>


          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
                <a-divider type="vertical" />
                <a @click="openOxtallForm(record)">评估表</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <basf-servicerecord-insects-service-record-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <basf-servicerecord-insects-service-evaluation-form v-if="showOxtallFormEdit" v-model:visible="showOxtallFormEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { InsectsServiceRecordApi } from '@/api/basf/InsectsServiceRecordApi';
import { message } from 'ant-design-vue';
import {RanchApi} from "@/api/property/RanchApi";
import BasfServicerecordInsectsServiceRecordEdit from './insects-service-record-edit.vue'
import moment from "moment";
import BasfServicerecordInsectsServiceEvaluationForm from './insects-service-evaluation-form.vue'

export default {
  name: 'BasfServicerecordInsectsServiceRecord',
  components: {
    PlusOutlined,
    BasfServicerecordInsectsServiceRecordEdit,
    BasfServicerecordInsectsServiceEvaluationForm
  },
  async mounted() {
    RanchApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.ranchList = resp.data;
      }
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '记录ID',
          dataIndex: 'bsrId'
        },
        {
          title: '牧场',
          dataIndex: 'ranchName',
          width: 150,
        },
        {
          title: '灭杀场所',
          dataIndex: 'regionTarget',
          customRender: function(row) {
            if (row.text == 1) {
              return "牛场";
            } else if (row.text == 2) {
              return "鸡鸭厂";
            } else {
              return "未知";
            }
          }
        },
        {
          title: '灭杀目标',
          dataIndex: 'killTarget',
          customRender: function(row) {
            if (row.text == 1) {
              return "灭鼠";
            } else if (row.text == 2) {
              return "灭蝇";
            } else {
              return "未知";
            }
          }
        },
        {
          title: '服务类型',
          dataIndex: 'serviceType',
          customRender: function(row) {
            if (row.text == 1) {
              return "首次服务";
            } else if (row.text == 2) {
              return "应急服务";
            } else if (row.text == 3) {
              return "常规服务";
            } else {
              return "未知";
            }
          }
        },
        {
          title: '开始时间',
          dataIndex: 'beginTime'
        },
        {
          title: '结束时间',
          dataIndex: 'endTime'
        },
        {
          title: '工作时长',
          dataIndex: 'endTime',
          customRender: function(row) {
            if (row.record.endTime == null) {
              return "未结束";
            }
            let begin = row.record.beginTime;
            let end = row.record.endTime;
            let mi = Math.floor((moment(end).diff(moment(begin))) / 1000);
            if (mi > 3600) {
              const hours = Math.floor(mi / 3600); // 3600秒 = 1小时
              const minutes = Math.floor((mi % 3600) / 60); // 余下的秒数除以60得到分钟数
              return hours + "小时" + minutes + "分";
            } else if (mi > 60) {
              return Math.ceil(mi / 60) + "分";
            } else {
              return mi + "秒";
            }
          }
        },
        {
          title: '执行用户',
          dataIndex: 'serviceUserName'
        },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      ranchList : [],
      showOxtallFormEdit : false,
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    reset() {
      this.where.ranchId = '';
      this.where.serviceType = '';
      this.where.regionTarget = '';
      this.where.killTarget = ''
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    async remove(row) {
      const result = await InsectsServiceRecordApi.delete({ bsrId: row.bsrId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    openOxtallForm(row) {
      this.current = row;
      this.showOxtallFormEdit = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return InsectsServiceRecordApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },

  }
};
</script>
